package org.devio.`as`.proj.main.http

import org.devio.`as`.proj.common.utils.SPUtil
import org.devio.hi.library.restful.HiRestful

object ApiFactory {
    // https://mock.yonyoucloud.com/mock/27713/android/dev/category/categories
    val KEY_DEGRADE_HTTP = "degrade_http"
    val HTTPS_BASE_URL = "https://mock.yonyoucloud.com/mock/27713/android/dev/"
    val HTTP_BASE_URL = "http://mock.yonyoucloud.com/mock/27713/android/dev/"
    val degrade2Http = SPUtil.getBoolean(KEY_DEGRADE_HTTP)
    val baseUrl = if (degrade2Http) HTTP_BASE_URL else HTTPS_BASE_URL
    private val hiRestful: HiRestful = HiRestful(baseUrl, RetrofitCallFactory(baseUrl))
    init {
        hiRestful.addInterceptor(BizInterceptor())
        // 需要登录、Token失效等错误码拦截
        hiRestful.addInterceptor(HttpStatusInterceptor())
        // 设置默认不降级
        SPUtil.putBoolean(KEY_DEGRADE_HTTP,false)
    }

    fun <T> create(service: Class<T>): T {
        return hiRestful.create(service)
    }
}